﻿using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IntelligentEscalator.DAL;

namespace IntelligentEscalator.BUS
{
    public class SqlHelper<T>
    {
        public static bool SqlExecute(string strsql)
        {
            try
            {
                using (var conn = DataFactory.GetSQLConnection())
                {
                    int count = conn.Execute(strsql);

                    if (count >= 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static int SqlExecuteRows(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                int count = conn.Execute(strsql,new { });

                return count;
            }
        }

        public static List<T> SqlList(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetListData(strsql);
            }
        }

        public static int SqlScalar(string strsql)
        {
            int count = 0;
            using (var conn = DataFactory.GetSQLConnection())
            {
                count = conn.ExecuteScalar<int>(strsql);
            }
            return count;
        }

        public static T SqlSingleData(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetData(strsql);
            }
        }

        public static bool SqlDelete(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {

                int count = conn.Execute(strsql);

                if (count >= 0)
                {
                    return true;
                }
                return false;
            }
        }

        public static string SqlScalarString(string strsql)
        {
            string str;

            using (var conn = DataFactory.GetSQLConnection())
            {
                str = conn.ExecuteScalar<string>(strsql);
            }
            return str;
        }

        public static List<T> SqlList(MySqlCommand mySqlCommand)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetListData(mySqlCommand.CommandText);
            }
        }

        public static int MySqlExecute(MySqlCommand command)
        {
            int count = 0;
            try
            {
                using (MySqlConnection connection = DataFactory.GetMysqlConnection())
                {
                    connection.Open();

                    // 创建SqlCommand对象

                    // 设置SqlCommand对象的连接和查询
                    command.Connection = connection;

                    // 执行非查询命令
                    count = command.ExecuteNonQuery();
                }
            }
            catch
            {
                count = 0;
            }
            return count;
        }

    }
}
